//
//  ChallengeListController.swift
//  SCA
//
//  Created by tjun on 2025/9/16.
//

import UIKit

class ChallengeListController: BaseController {

    private var challengeCode: String = ""
    
    private var datas: [ChallengeListModel] = []

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = AppStyle.color.background
        tableView.rowHeight = 40
        tableView.contentInsetAdjustmentBehavior = .never;
        tableView.register(ChallengeListCell.self, forCellReuseIdentifier: ChallengeListCell.identifier)
        tableView.estimatedRowHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 0
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: tabBarSafeBottomMargin, right: 0)
        tableView.separatorStyle = .none
        return tableView
    }()

    private let codeLabel = UILabel(font: UIFontMediumMake(16), textColor: .white)
    private let totalNumberLabel = UILabel(font: UIFontMediumMake(15), textColor: .white)
    private let headerView = UIView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationBar.title = "承诺挑战列表"
        
        headerView.isHidden = true
        headerView.addTo(self.view)
        headerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navBarHeight)
            make.height.equalTo(80)
        }
        
        codeLabel.addTo(headerView)
        codeLabel.snp.makeConstraints { make in
            make.left.equalTo(AppStyle.margin.left + 12)
            make.top.equalTo(10)
        }
        
        let copyButton = UIButton(image: UIImage(named: "share_copy"))
        copyButton.addTarget(self, action: #selector(copyButtonTapped), for: .touchUpInside)
        copyButton.addTo(headerView)
        copyButton.snp.makeConstraints { make in
            make.left.equalTo(codeLabel.snp.right).offset(10)
            make.centerY.equalTo(codeLabel)
            make.height.width.equalTo(30)
        }
        
        totalNumberLabel.addTo(headerView)
        totalNumberLabel.snp.makeConstraints { make in
            make.right.equalTo(-AppStyle.margin.left - 12)
            make.centerY.equalTo(codeLabel)
        }
        
        let challengeListView = ChallengeListView().addTo(headerView)
        challengeListView.dateLabel.text = "日期".localizedString
        challengeListView.accountLabel.text = "绑定账户".localizedString
        challengeListView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.bottom.equalToSuperview()
            make.height.equalTo(36)
        }
        
        let lineView = UIView().addTo(headerView)
        lineView.backgroundColor = .white.withAlphaComponent(0.3)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        tableView.addTo(view)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(headerView.snp.bottom)
        }
        
        loadData()
    }
    
    @objc func copyButtonTapped() {
        UIPasteboard.general.string = challengeCode
        HUD.showMessage("复制成功".localizedString)
    }
    
    @objc func loadData() {
        MineService.loadChallengeData(success: { model in
            self.headerView.isHidden = false
            self.codeLabel.text = String(format: "代码: %@".localizedString, model.code)
            self.totalNumberLabel.text = "\(model.usedCodeNumber)/\(model.totalCodeNumber)"
            self.challengeCode = model.code
            self.datas.append(contentsOf: model.models)
            self.tableView.reloadData()
        }) { error in
            
        }
    }
 
}


extension ChallengeListController: UITableViewDataSource, UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ChallengeListCell.identifier, for: indexPath) as! ChallengeListCell
        cell.indexPath = indexPath
        let model = datas[indexPath.row]
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        
    }
    
}
